package JavaFoundation.myLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentryLockTest {
//    static Object objectLockA = new Object();
//    public static void m1(){
//        new Thread(()->{
//            synchronized (objectLockA) {
//                System.out.println(Thread.currentThread().getName() + "\t"+"---外层调用");
//                synchronized (objectLockA) {
//                    System.out.println(Thread.currentThread().getName() + "\t"+"---中层调用");
//                    synchronized (objectLockA){
//                        System.out.println(Thread.currentThread().getName()+"\t"+"---内层调用");
//                    }
//                }
//            }
//        },"t1").start();
//    }
//
//    public static void main(String[] args) {
//        m1();
//    }

    static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(()->{
            lock.lock();
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"/t"+"----外层调用");
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"/t"+"----内层调用");
                lock.unlock();
            }finally {
                lock.unlock();
              //  lock.unlock();
                //当一个线程没有释放锁的时候，其他的线程是无法得到这个锁的
            }

        },"t1").start();

        new Thread(()->{
            lock.lock();
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"/t"+"----外层调用");
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"/t"+"----内层调用");
                lock.unlock();
            }finally {
                lock.unlock();
                lock.unlock();
            }

        },"t2").start();
    }
}
